//
//  MainDetailViewController.m
//  WZYInteriorDesign
//
//  Created by lanou on 15/12/17.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "MainDetailViewController.h"
#import "MainDetailModel.h"
#import "MainCollectionViewCell.h"
#import "MainLabelModel.h"
#import "HeadCollectionReusableView.h"
#import "PictureViewController.h"
#import "UIScrollView+MJRefresh.h"

@interface MainDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

//数组接收图片
@property (nonatomic, retain)NSMutableArray *mainArray;
//集合视图
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)MainDetailModel *mainDetail;

@end

@implementation MainDetailViewController
- (NSMutableArray *)mainArray {
    if (!_mainArray) {
        _mainArray = [[NSMutableArray alloc]init];
    }
    return _mainArray;
}
//解析标头视图
- (void)jsonHead{
    if (self.mailLabel.author_nick) {
        NSString *urlString = [NSString stringWithFormat:@"%@%@%@",kMainDetail1,self.mailLabel.ID,kMainDetail2];
    [LORequestManger GET:urlString success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *headDic1 = dic[@"data"][@"collection_info"][@"info"];
       self.mainDetail = [MainDetailModel shareJsonWithDictonary:headDic1];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    }
    
}


//解析集合视图
- (void)jsonCollectionView{
//    if (self.mailLabel.author_nick) {

    NSString *urlString = [NSString stringWithFormat:@"%@%@%@",kMainDetail1,self.mailLabel.ID,kMainDetail2];
    [LORequestManger GET:urlString success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        NSArray *rootArray = dic[@"data"][@"collection_photos2"][@"photos"];
        NSArray *rootArray2 = rootArray[0][@"value"];
            for (NSDictionary *imageDic in rootArray2) {
                MainDetailModel *mainDetail = [MainDetailModel shareJsonWithDictonary:imageDic];
                [self.mainArray addObject:mainDetail];
            }
        //NSLog(@"%@",self.mainArray);
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
}
//}

- (void)viewDidAppear:(BOOL)animated
{
    [self jsonCollectionView];
    //[self jsonHead];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self jsonHead];
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //创建布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //列距
    flowLayout.minimumInteritemSpacing = 20;
    //行距
    flowLayout.minimumLineSpacing = 10;
    //分区内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //itemSize 1行2列
    CGFloat totalWidth = self.view.frame.size.width;
    CGFloat itemWidth = (totalWidth - 2*20) / 2.0;
    CGFloat itemHeight = 1.3*itemWidth;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    //滚动方法 竖直滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //区头大小
    flowLayout.headerReferenceSize = CGSizeMake(0, 250);
    //创建collectionView对象,赋值布局
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    //设置数据源和代理
    self.collectionView.dataSource =self;
    self.collectionView.delegate = self;
    
    //注册单元格
    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];
    //注册区头
    [self.collectionView registerClass:[HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //添加返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(15, 15, 30, 30);
    [backButton addTarget:self action:@selector(PushToMainVc) forControlEvents:(UIControlEventTouchUpInside)];
    [backButton setImage:[UIImage imageNamed:@"arrowdown.png"] forState:(UIControlStateNormal)];
    [backButton setTintColor:[UIColor cyanColor]];
    [self.collectionView addSubview:backButton];
    [self.view addSubview:self.collectionView];
    
    __weak MainDetailViewController *mainView = self;
    
    //上拉刷新
    [mainView.collectionView addHeaderWithCallback:^{
        NSLog(@"上拉刷新");
        [mainView jsonCollectionView];
        [mainView.collectionView headerEndRefreshing];
    }];
    
    // Do any additional setup after loading the view.
}

#pragma mark-
#pragma mark-------------------数据源方法--------
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


//分区中item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.mainArray.count;
   
}

#pragma mark-
#pragma mark-------------------加载单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MainCollectionViewCell *cell1 = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
    MainDetailModel *mainDetail = _mainArray[indexPath.row];
    cell1.mainDetail = mainDetail;
    return cell1;
}
#pragma mark-
#pragma mark-------------------加载区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    HeadCollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
     header.mainDetail = self.mainDetail ;
    return header;

}

//点击后进入图片详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureViewController *pictureVC = [[PictureViewController alloc]init];
    pictureVC.mainDail = self.mainArray[indexPath.item];
    [self presentViewController:pictureVC animated:YES completion:nil];
}

//实现返回button方法
- (void)PushToMainVc{
    //MainDetailViewController *mainVC = [[MainDetailViewController alloc]init];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
